//关闭计时器 
function closeTimer(jishiqi) {
	window.clearInterval(jishiqi);
}

//等待司机接单时候的动画展示 
function waitDriver() {
	/*
	 * $("body").append("<div id='waitDriver' class=\"inputAddressBox\"
	 * style=\"background:#000;opacity:0.1;position: fixed;top:0;left:0;\"></div>");
	 * $("body").append("<div class=\"inputAddressBox\" style=\"position:
	 * fixed;top:0;left:0;color:red;\">asdas</div>");
	 * $(document).ready(function(){ $("#jindutiao").animate({ width:'100%'
	 * },20000); });
	 */

	$("body")
			.append(
					"<div id='waitDriver' class=\"inputAddressBox wait\" style=\"background:#000;opacity:0.1;position: fixed;top:0;left:0;\"></div>");
	$("body")
			.append(
					"<div class=\"inputAddressBox wait\" style=\"position: fixed;top:0;left:0;\"><div class='waitBox'><div class='waitBox-header'>等待司机接单</div><div class='waitBox-content'><p class='vmiddle'><span class='fa-stack fa-lg'><i style='-webkit-animation:fa-spin 5s infinite linear;animation:fa-spin 5s infinite linear;' class='fa fa-ban text-danger fa-li fa fa-spinner fa-spin'></i></span> 等待接单中...</p></div><div class='waitBox-foot'><a id='cancelTrip'  style='color:red;'>取消订单</a></div></div></div>");
}

/* 取消行程 */
function cancelTrip(tripId) {
	$.ajax({
		url : "/Wchat/functionalBusinessController/cancelTrip.do?tripId="
				+ tripId,
		success : function(json) {
			$(".wait").remove();// 去除时间等待动画
			closeTimer(jishiqi);// 顺便关闭计时器
		}
	});
}

//轮询服务端,获取接单的司机
function getTrip(json) {
	tripId = json.id;// 乘客的行程ID
	jishiqi = setInterval(
			function() {
				$
						.ajax({
							url : "/Wchat/functionalBusinessController/getTrip.do?tripId="
									+ json.id,
							success : function(json) {
								if (json.success == true) {
									closeTimer(jishiqi);
									$(".waitBox>.waitBox-content .vmiddle")
											.html(
													"<a style='color:green;font-size:12px;'>有司机接单,即将跳转到支付界面</a>");
									window.location.href = "个人中心-拼车.html?orderId="
											+ json.orderId;
								}
							}
						});
			}, 2000);
}

/*
 * 发布拼车信息 需要如下参数 ： 
 * @param startAddress 		开始位置 
 * @param startDetailAddress开始位置详情
 * @param startLatitude 	开始位置坐标 纬度
 * @param startLongitude 	开始位置坐标 经度 
 * @param endAddress 		结束位置 
 * @param endDetailAddress  开始位置详情 
 * @param endLatitude 		结束位置坐标 纬度
 * @param endLongitude  	结束位置坐标 经度 
 * @param miles 			里程(公里数) 
 * @param estimateCost 		预计费用
 * @param planStartDatetime 预计开始时间 
 * @param planEndDatetime 	预计到达时间 
 * @param passengerNums 	乘客人数
 */
function submitCarRentalInfo() {
	//waitDriver();
	/*console.log(startAddress);
	console.log(startDetailAddress);
	console.log(endAddress);
	console.log(endDetailAddress);
	console.log("开始纬度:" + start_lat + " ;开始经度:" + start_lng);
	console.log("结束纬度:" + end_lat + " ;结束经度:" + end_lng);
	console.log("预计费用:" + estimateCost);
	console.log("里程:" + miles);*/
	planStartDatetime=$("#planStartDatetime").val();
	if (start_lat == null || start_lat == "") {
		return;
	}

	if (start_lng == null || start_lng == "") {
		return;
	}

	if (end_lat == null || end_lat == "") {
		return;
	}

	if (end_lng == null || end_lng == "") {
		return;
	}
	
	
	var url = "/Wchat/functionalBusinessController/findNearVehicle.do?startAddress="
			+ startAddress + "&startDetailAddress=" + startDetailAddress
			+ "&startLatitude=" + start_lat + "&startLongitude=" + start_lng
			+ "&endDetailAddress=" + endDetailAddress + "&endAddress="
			+ endAddress + "&endLatitude=" + end_lat + "&endLongitude="
			+ end_lng + "&passengerNums=1&miles=" + miles + "&estimateCost="
			+ estimateCost+"&planStartDatetime="+planStartDatetime;
	console.log(url);
	$.ajax({
		url : url,
		success : function(json) {
			//getTrip(json);
			/*console.log(json);*/
			window.location.href="rentVehicle.html";//跳转到车辆列表界面
		}
	});
}

/*地址录入*/
function addLuru() {
	document.getElementsByClassName("inputAddressBox")[0].style.display = "block";
}

/* 关闭弹出窗口 */
function canel(clas) {
	document.getElementsByClassName(clas)[0].style.display = "none";
}

/* 登录 */
function login() {
	$.ajax({
		url : "/Wchat/functionalBusinessController/login.do",
		success : function(json) {
			
		}
	});
}

/* 初始化地址列表 */
function initAddressList(keyword) {
	$.ajax({
		url : "/Wchat/AuxiliaryController/addressExchange.do?keyword="
				+ keyword,
		success : function(json) {

			if (json.status == false) {
				return;
			}

			var list = json.content.data;
			$("#srarchAddressResult").empty();
			if (list.length < 1) {
				return;
			}
			var str = "";
			for (var i = 0; i < list.length; i++) {
				str = str + "<li class='address' startAddress='"
						+ list[i].title + "' startDetailAddress='"
						+ list[i].address + "' endlat='" + list[i].location.lat
						+ "' endlng='" + list[i].location.lng
						+ "'><span class=\"addressMain\">" + list[i].title
						+ "</span><span class=\"addressFu\">" + list[i].address
						+ "</span></li>";
			}

			$("#srarchAddressResult").append(str);
		}
	});
}

//关闭地图匹配界面
function closeMap() {
	$("#container").hide();
}

/* 计算费用并显示值到固定元素 */
function calculatingCost() {

	if (start_lat == null || start_lat == "") {
		return;
	}

	if (start_lng == null || start_lng == "") {
		return;
	}

	if (end_lat == null || end_lat == "") {
		return;
	}

	if (end_lng == null || end_lng == "") {
		return;
	}

	$.ajax({
		url : "/Wchat/functionalBusinessController/calculatingCost.do",
		data : {
			start_lat : start_lat,
			start_lng : start_lng,
			end_lat : end_lat,
			end_lng : end_lng
		},
		success : function(json) {
			
		}
	});
}
